package com.qiyun.framework.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.qiyun.framework.helper.BitmapHelper;

import java.util.List;

/**
 * Created by mac on 16/3/22.
 */
public abstract class CommonRecyclerAdapter<T> extends RecyclerView.Adapter<CommonRecyclerAdapter.ViewHolder>{
    private Context mContext;
    private List<T> mList;
    protected final int mItemLayoutId;

    public CommonRecyclerAdapter(Context context, List<T> mList, int itemLayoutId) {
        this.mContext = context;
        this.mList = mList;
        this.mItemLayoutId=itemLayoutId;
    }

    public void setList(List<T> list){
        mList=list;
        notifyDataSetChanged();
    }

    public List<T> getList(){
        return mList;
    }


    //创建新View，被LayoutManager所调用
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(this.mItemLayoutId,viewGroup,false);
        return new ViewHolder(view);
    }

    public abstract void bindView(ViewHolder holder, T item);

    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {
        bindView(viewHolder,mList.get(position));
    }
    //获取数据的数量
    @Override
    public int getItemCount() {
        return mList.size();
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder{
        private View mView;
        public ViewHolder(View view){
            super(view);
            this.mView=view;
        }

        public View getRootView(){
            return mView;
        }
        /**
         * 对textView简单控件进行封装
         */
        public void setText(int layoutId,String str){
            try {
                ((TextView)getView(layoutId)).setText(str);
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        /**
         * 对ImageView简单控件进行封装
         */
        public void setImage(int layoutId,int imgRes){
            try {
                ((ImageView)getView(layoutId)).setImageResource(imgRes);
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        /**
         *  对ImageView简单控件进行封装网络获取
         * @param layoutId
         * @param imgUrl image的网络url
         */
        public void setImage(int layoutId,String imgUrl){
            try {
                BitmapHelper.getBitmapUtils().bind(((ImageView) getView(layoutId)),imgUrl);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        /**
         * 通过控件的Id获取对于的控件，如果没有则加入views
         *
         * @param viewId
         * @return
         */
        public <T extends View> T getView(int viewId) {
            View view = mView.findViewById(viewId);
            return (T) view;
        }
    }
}
